<?php
include ("conn.php");
$skey = $_POST['skey'];
//验证skey是否存在
$sql="SELECT * FROM user WHERE thirdSession='".$skey."'";
$result = $conn->query($sql);
if ($result->num_rows == 0) {exit('{"code":450}');}
$row = $result->fetch_assoc();
$uid = $row['id'];

$pid = $_POST['pid'];
$content = $_POST['content'];
//首先拿到pcoid
$sql="SELECT MAX(pcoid) FROM comment WHERE pid=".$pid;
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if(json_encode($row['MAX(pcoid)'])=="null"){
	$pcoid = 1;
}else{
	$pcoid = (int)$row['MAX(pcoid)']+1;
}
//处理评论内容
$patterns="/    + /";
$replacements="    ";
$content = preg_replace($patterns, $replacements, $content);
$patterns="/\n\n+\n/";
$replacements="<br><br>";
$content = preg_replace($patterns, $replacements, $content);
$patterns="/\n/";
$replacements="<br>";
$content = preg_replace($patterns, $replacements, $content);
$sql="INSERT INTO comment (uid, pid, pcoid, content,likeNum,date)
VALUES (".$uid.",".$pid.",".$pcoid.",'".$content."',0,'".date("Y-m-d H:i:s")."')";
$result = $conn->query($sql);
echo '{"code":"ok"}';
?>